import _Table from "choerodon-ui/pro/lib/table";
import _Row from "choerodon-ui/pro/lib/row";
import _Col from "choerodon-ui/pro/lib/col";
import _Form from "choerodon-ui/pro/lib/form";
import _Button from "choerodon-ui/pro/lib/button";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _DataSet from "choerodon-ui/pro/lib/data-set";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _class, _class2;

/**
 * @since 2019-10-10
 */
import React from 'react';
import { Bind } from 'lodash-decorators';
import { Content, Header } from 'components/Page';
import intl from 'utils/intl';
import { TagRender } from 'utils/renderer';
import { CPAAS_IMP } from 'utils/config';
import notification from 'utils/notification';
import { openTab } from 'utils/menuTab';
import formatterCollections from 'utils/intl/formatterCollections';
import { filterNullValueObject, getCurrentOrganizationId } from 'utils/utils';
import queryString from 'querystring';
var ImportHistory = (_dec = formatterCollections({
  code: ['cimp.commentImport', 'cimp.importHistory']
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(ImportHistory, _React$Component);

  var _super = _createSuper(ImportHistory);

  function ImportHistory(props) {
    var _this;

    _classCallCheck(this, ImportHistory);

    _this = _super.call(this, props);
    _this.tableDS = new _DataSet({
      autoQuery: false,
      dataKey: 'content',
      selection: false,
      queryFields: [{
        name: 'templateCode',
        type: 'object',
        label: intl.get('cimp.importHistory.view.title.importPlatform').d('导入模板'),
        lovCode: 'CIMP.TEMPLATE',
        lovPara: {
          tenantId: getCurrentOrganizationId()
        },
        required: true,
        noCache: true
      }, {
        name: 'creationDateFrom',
        type: 'dateTime',
        label: intl.get('cimp.importHistory.view.title.createIncidentFrom').d('创建时间从')
      }, {
        name: 'creationDateTo',
        type: 'dateTime',
        label: intl.get('cimp.importHistory.view.title.createIncidentTo').d('创建时间至')
      }],
      fields: [{
        name: 'batch',
        type: 'string',
        label: intl.get('cimp.importHistory.view.title.importBatch').d('导入批次号')
      }, {
        name: 'dataCount',
        type: 'string',
        label: intl.get('cimp.importHistory.view.title.totalData').d('数据总量')
      }, {
        name: 'status',
        type: 'string',
        label: intl.get('cimp.importHistory.view.title.status').d('状态')
      }, {
        name: 'createdUserName',
        type: 'string',
        label: intl.get('cimp.importHistory.view.title.createUser').d('创建人')
      }, {
        name: 'creationDate',
        type: 'string',
        label: intl.get('cimp.importHistory.view.title.createTime').d('创建时间')
      }],
      transport: {
        read: function read(config) {
          var _config$data = config.data,
              _config$data$creation = _config$data.creationDateFrom,
              creationDateFrom = _config$data$creation === void 0 ? '' : _config$data$creation,
              _config$data$creation2 = _config$data.creationDateTo,
              creationDateTo = _config$data$creation2 === void 0 ? '' : _config$data$creation2;

          var _ref = config.data.templateCode ? config.data.templateCode : {},
              _ref$templateCode = _ref.templateCode,
              templateCode = _ref$templateCode === void 0 ? {} : _ref$templateCode,
              _ref$prefixPatch = _ref.prefixPatch,
              prefixPatch = _ref$prefixPatch === void 0 ? '' : _ref$prefixPatch,
              _ref$templateType = _ref.templateType,
              templateType = _ref$templateType === void 0 ? '' : _ref$templateType;

          var data = filterNullValueObject({
            templateCode: templateCode,
            creationDateFrom: creationDateFrom,
            creationDateTo: creationDateTo
          });

          var params = _objectSpread({}, config.params);

          var url = templateType === 'C' ? "".concat(prefixPatch, "/v1/").concat(getCurrentOrganizationId(), "/import/manager") : "".concat(CPAAS_IMP, "/v1/").concat(getCurrentOrganizationId(), "/import/manager");
          return {
            data: data,
            params: params,
            url: url,
            method: 'GET'
          };
        }
      }
    });

    _this.renderBar = function (_ref2) {
      var queryFields = _ref2.queryFields,
          buttons = _ref2.buttons,
          queryFieldsLimit = _ref2.queryFieldsLimit,
          dataSet = _ref2.dataSet,
          queryDataSet = _ref2.queryDataSet;

      if (queryDataSet) {
        return /*#__PURE__*/React.createElement(_Form, {
          columns: queryFieldsLimit,
          dataSet: queryDataSet
        }, queryFields, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(_Button, {
          onClick: function onClick() {
            return _this.clearContent();
          }
        }, intl.get('cpaas.common.button.reset').d('重置')), /*#__PURE__*/React.createElement(_Button, {
          dataSet: null,
          color: "primary",
          onClick: function onClick() {
            queryDataSet.validate().then(function (val) {
              if (val && queryDataSet.current && queryDataSet.current.get('templateCode')) {
                dataSet.query();
              } else {
                notification.error({
                  message: intl.get('cimp.modelType.model.modelType.notificationErrMsg').d('存在必输字段未填写！')
                });
              }
            });
          }
        }, intl.get('cpaas.common.button.search').d('查询')), buttons));
      }
    };

    _this.state = {
      id: null,
      flag: true,
      prefixPatch: ''
    };
    return _this;
  }

  _createClass(ImportHistory, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.tableDS.queryDataSet.addEventListener('update', this.templateCodeQuery);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.tableDS.queryDataSet.removeEventListener('update', this.templateCodeQuery);
    }
  }, {
    key: "templateCodeQuery",
    value: function templateCodeQuery() {
      var flag = this.state.flag;

      if (this.tableDS.queryDataSet.current && this.tableDS.queryDataSet.current.get('templateCode')) {
        var _this$tableDS$queryDa = this.tableDS.queryDataSet.current.get('templateCode'),
            id = _this$tableDS$queryDa.id,
            prefixPatch = _this$tableDS$queryDa.prefixPatch;

        this.setState({
          prefixPatch: prefixPatch
        });

        if (id !== this.state.id || flag) {
          this.tableDS.query();
          this.setState({
            id: id,
            flag: false
          });
        }
      }
    }
    /**
     * 历史记录详情页
     * @param {*} record
     */

  }, {
    key: "openDetailPage",
    value: function openDetailPage(record) {
      // 跳转到当前行详情页
      var _record$data = record.data,
          _record$data$importId = _record$data.importId,
          importId = _record$data$importId === void 0 ? '' : _record$data$importId,
          _record$data$template = _record$data.templateCode,
          templateCode = _record$data$template === void 0 ? '' : _record$data$template,
          _record$data$batch = _record$data.batch,
          batch = _record$data$batch === void 0 ? '' : _record$data$batch;
      var _this$state$prefixPat = this.state.prefixPatch,
          prefixPatch = _this$state$prefixPat === void 0 ? '' : _this$state$prefixPat;
      openTab({
        key: "/cimp/history/detail/".concat(importId, "/").concat(templateCode, "/").concat(batch),
        title: 'cpaas.common.title.historyDetail',
        closable: true,
        search: queryString.stringify({
          action: intl.get('cpaas.common.title.historyDetail').d('历史详情'),
          prefixPatch: prefixPatch
        })
      });
    }
  }, {
    key: "clearContent",
    value: function clearContent() {
      this.tableDS.queryDataSet.current.set({
        creationDateFrom: null,
        creationDateTo: null
      });
    }
  }, {
    key: "columns",
    get: function get() {
      return [{
        name: 'batch'
      }, {
        name: 'dataCount'
      }, {
        name: 'status',
        renderer: function renderer(records) {
          var statusList = [{
            status: 'UPLOADING',
            color: 'blue'
            /* , text: 'Excel导入' */

          }, {
            status: 'UPLOADED',
            color: 'green'
            /* , text: '验证成功' */

          }, {
            status: 'CHECKING',
            color: 'blue'
            /* , text: '验证失败' */

          }, {
            status: 'CHECKED',
            color: 'green'
            /* , text: '导入成功' */

          }, {
            status: 'IMPORTING',
            color: 'blue'
            /* , text: '导入失败' */

          }, {
            status: 'IMPORTED',
            color: 'green'
            /* , text: '数据异常' */

          }];
          var tagItem = statusList.find(function (t) {
            return t.status === records.value;
          }) || {};
          return TagRender(records.value, [{
            status: records.value,
            text: records.record.data.statusMeaning,
            color: tagItem.color
          }]);
        }
      }, {
        name: 'createdUserName'
      }, {
        name: 'creationDate'
      }, {
        header: intl.get('cpaas.common.table.column.option').d('操作'),
        width: 200,
        renderer: this.commands,
        lock: 'right',
        align: 'center'
      }];
    }
  }, {
    key: "commands",
    value: function commands(_ref3) {
      var _this2 = this;

      var record = _ref3.record;
      return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(_Row, null, /*#__PURE__*/React.createElement(_Col, null, /*#__PURE__*/React.createElement("span", {
        className: "action-link"
      }, /*#__PURE__*/React.createElement("a", {
        onClick: function onClick() {
          return _this2.openDetailPage(record);
        }
      }, intl.get('cimp.commentImport.view.button.detail').d('详情'))))));
    }
  }, {
    key: "render",
    value: function render() {
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get('cimp.commentImport.view.message.title').d('导入历史')
      }), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement(_Table, {
        dataSet: this.tableDS,
        columns: this.columns,
        queryFieldsLimit: 3 // queryBar={this.renderBar}
        ,
        queryFields: {
          templateCode: {
            clearButton: false
          }
        }
      })));
    }
  }]);

  return ImportHistory;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "templateCodeQuery", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "templateCodeQuery"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "openDetailPage", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "openDetailPage"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "clearContent", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "clearContent"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "commands", [Bind], Object.getOwnPropertyDescriptor(_class2.prototype, "commands"), _class2.prototype)), _class2)) || _class);
export { ImportHistory as default };